<?php

namespace app\admin\controller\choi;

use app\admin\model\choi\Setting;
use app\common\controller\Backend;
use think\Db;
use app\admin\model\choi\Teacher as TeacherModel;
use app\admin\model\choi\TeacherMoneyLog as TeacherMoneyLogModel;

/**
 * 教师提现管理
 *
 * @icon fa fa-circle-o
 */
class TeacherWithdraw extends Backend
{
    
    /**
     * TeacherWithdraw模型对象
     * @var \app\admin\model\choi\TeacherWithdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\choi\TeacherWithdraw;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("withdrawFee", Setting::getValue('withdraw_fee'));
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['teacher'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['teacher'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 拒绝提现
     */
    public function refuse()
    {
        $id = $this->request->post('id');
        $teacherWithdraw = $this->model->find($id);
        if (!$teacherWithdraw) {
            $this->error('提现记录不存在');
        }
        if ($teacherWithdraw->status != 1) {
            $this->error('提现已被处理，无需再次操作');
        }
        $teacher = TeacherModel::find($teacherWithdraw->teacher_id);
        if (!$teacher) {
            $this->error('教师不存在');
        }
        Db::startTrans();
        try {
            //更新提现状态
            $teacherWithdraw->status = 2;
            $teacherWithdraw->save();
            //账户明细记录
            TeacherMoneyLogModel::add($teacher, $teacherWithdraw->money);
            //回滚账号余额
            $teacher->setInc('money', $teacherWithdraw->money);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        isset($e) ? $e->getMessage() : $this->success('操作成功');
    }

    /**
     * 同意
     */
    public function agree()
    {
        $id = $this->request->post('id');
        $teacherWithdraw = $this->model->find($id);
        if (!$teacherWithdraw) {
            $this->error('提现记录不存在');
        }
        if ($teacherWithdraw->status != 1) {
            $this->error('提现已被处理，无需再次操作');
        }
        $teacherWithdraw->status = 3;
        $res = $teacherWithdraw->save();
        $res ? $this->success('操作成功') : $this->error('操作失败,请联系管理员');
    }

    /**
     * 提现手续费
     */
    public function fee()
    {
        $fee = $this->request->post('fee/d');
        if ($fee < 0 || $fee > 100) {
            $this->error('请输入0~100的数字');
        }
        $withdrawFee = Setting::getValue('withdraw_fee');
        if ($withdrawFee == null) {
            $res = Setting::add('withdraw_fee', $fee, '提现手续费（百分比）');
        } else {
            if ($withdrawFee == $fee) {
                $res = true;
            } else {
                $res = Setting::updateValue('withdraw_fee', $fee);
            }
        }
        $res ? $this->success('设置成功') : $this->error('设置失败,请联系管理员');
    }

    /**
     * 提现说明
     */
    public function explain()
    {
        $withdrawExplain = Setting::getValue('withdraw_explain');
        if ($this->request->isPost()) {
            $explain = $this->request->post('explain');
            if ($withdrawExplain == null) {
                $res = Setting::add('withdraw_explain', $explain, '提现说明');
            } else {
                if ($withdrawExplain == $explain) {
                    $res = true;
                } else {
                    $res = Setting::updateValue('withdraw_fee', $explain);
                }
            }
            $res ? $this->success('设置成功') : $this->error('设置失败,请联系管理员');
        }

        $this->assign('explain', $withdrawExplain);
        return $this->fetch();
    }
}
